﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChairtyBank.DAL;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;



namespace ChairtyBank
{
    public partial class MainWindow : Form
    {
        //private int childFormNumber = 0;
        public UserEntity user;
        public LoginForm loginForm;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            CustomerForm customerForm = new CustomerForm();
            customerForm.MdiParent = this;
            customerForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            InstalmentForm form = new InstalmentForm();
            form.MdiParent = this;
            form.Show();
            form.user = user.Id;
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }





        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            ReportsForm form = new ReportsForm();
            form.MdiParent = this;
            form.Visible = true;
        }

        private void closeAll_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            testCustomerForm customerForm = new testCustomerForm();
            customerForm.MdiParent = this;
            customerForm.Show();
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.loginForm.Visible = true;
            this.loginForm.clearFields();
            this.Dispose();
            this.Visible = false;

        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            if (user.UType == User.NORMAL_USER_UType)
            {
                this.addNewDataButton.Enabled = false;
                tscBackup.Enabled = false;
            }
            if (user.UType == User.SEARCHER_USER_UTYPE)
            {
                this.addNewDataButton.Enabled = false;
                this.InstalmentDataButton.Enabled = false;
                this.ReportsFormButton.Enabled = false;
                tscBackup.Enabled = false;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            PreAcceptForm preAcceptform = new PreAcceptForm();
            preAcceptform.MdiParent = this;
            preAcceptform.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            PhotosForm form = new PhotosForm();
            form.MdiParent = this;
            form.Show();
        }



        private void استرجاعToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = false;
            openFile.DefaultExt = "bak";
            // The Filter property requires a search string after the pipe ( | )
            openFile.Filter = "DataBase Backup File (*.bak)|*.bak";
            openFile.ShowDialog();
            if (openFile.FileNames.Length > 0)
            {
                MessageBox.Show(openFile.FileName.ToString());

                try
                {
                    string CharityConString = ConfigurationManager.ConnectionStrings["ChairtyBank"].ToString();
                    string ElkhairConString = ConfigurationManager.ConnectionStrings["BankElKheir"].ToString();
                    if (File.Exists(openFile.FileName))
                    {
                        if (openFile.FileName.Contains("CharityBank") || openFile.FileName.Contains("BankElKheir"))
                        {
                            if (openFile.FileName.Contains("CharityBank"))
                            {
                                if (DBAccess.RestoreDB(CharityConString, openFile.FileName))
                                    MessageBox.Show("تمت العملية بنجاح");
                                else
                                    MessageBox.Show("عذرا لم تتم العملية بنجاح");
                            }
                            else if (openFile.FileName.Contains("BankElKheir"))
                            {
                                if (DBAccess.RestoreDB(ElkhairConString, openFile.FileName))
                                    MessageBox.Show("تمت العملية بنجاح");
                                else
                                    MessageBox.Show("عذرا لم تتم العملية بنجاح");
                            }
                        }
                        else
                        {
                            MessageBox.Show("اسم الملف غير صحيح");
                        }
                    }

                }
                catch
                {
                    MessageBox.Show("حدث خطأ اثناء العملية , برجاء الاتصال بالدعم الفني");
                }
            }
        }

        private void نسخهاحتياطيهToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.ShowNewFolderButton = true;
            DialogResult result = folderDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    string CharityConString = ConfigurationManager.ConnectionStrings["ChairtyBank"].ToString();
                    string ElkhairConString = ConfigurationManager.ConnectionStrings["BankElKheir"].ToString();
                    string CharityFileName = "\\CharityBank-" + DateTime.Today.ToString("dd-MM-yyyy");
                    string ElkhairFileName = "\\BankElKheir-" + DateTime.Today.ToString("dd-MM-yyyy");
                    if (File.Exists(folderDlg.SelectedPath + CharityFileName + ".bak"))
                    {
                        CharityFileName += "(" + Directory.GetFiles(folderDlg.SelectedPath, "CharityBank*").Length + ")";
                    }
                    if (File.Exists(folderDlg.SelectedPath + ElkhairFileName + ".bak"))
                    {
                        ElkhairFileName += "(" + Directory.GetFiles(folderDlg.SelectedPath, "BankElKheir*").Length + ")";
                    }
                    DBAccess.CreateBackup(CharityConString, folderDlg.SelectedPath + CharityFileName);
                    DBAccess.CreateBackup(ElkhairConString, folderDlg.SelectedPath + ElkhairFileName);
                    MessageBox.Show("تمت العملية بنجاح");
                }
                catch
                {
                    MessageBox.Show("حدث خطأ اثناء العملية , برجاء الاتصال بالدعم الفني");
                }
            }

        }
    }
}
